package com.liang;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liang.mapper.UserMapper;
import com.liang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

/**
 * @program: mybatis_plus
 * @description: 条件构造器
 * @author: Mr.Liang
 * @create: 2021-08-02 10:24
 **/
@SpringBootTest
public class WrapperText {
    private UserMapper userMapper;
    @Autowired(required=false)
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Test
    void contextLoads() {
        //查询name不为空，邮箱不为空，年龄大于等于12岁的
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.isNotNull("name")
                .isNotNull("email")
                .ge("age",12);
        List<User> objects = userMapper.selectList(objectQueryWrapper);
        objects.forEach(System.out::println);
    }

    @Test
    void contextLoads1(){
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("name","hello boy");
        User user = userMapper.selectOne(objectQueryWrapper);//查询一个对象
        System.out.println(user.toString());
    }

    @Test
    void contextLoads2(){
        //查询年龄在20到30岁之间的用户
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.between("age",20,30);//区间
        int count  = userMapper.selectCount(objectQueryWrapper);//查询结果数
        System.out.println(count);
    }

    @Test
    void contextLoads3(){
        //模糊查询
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.notLike("name","e")
                //左和右 ， e%右边
                        .likeRight("name","l");

         List<Map<String,Object>> map = userMapper.selectMaps(objectQueryWrapper);//查询结果数
        map.forEach(System.out::println);
    }

    @Test
    void contextLoads4(){
        //模糊查询
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.inSql("id","select id from user where id<2");

        List<Object> objects = userMapper.selectObjs(objectQueryWrapper);//查询结果数
        objects.forEach(System.out::println);
    }
}
